import requests
from lxml import etree
import pandas
import time

cookie = "lastCity=101280100; wd_guid=9dce353e-c512-48ec-8e12-dcdba1459936; historyState=state; _bl_uid=6hl6qpCbj1Ioen53pi8j1530pqnC; __fid=afde45ca4dfddb13b2d6d50449fef74f; __zp_seo_uuid__=a2639279-bf50-42c1-abff-bb08073feb29; __g=-; __l=r=https%3A%2F%2Fcn.bing.com%2F&l=%2Fwww.zhipin.com%2Fguangzhou%2F&s=1&g=&s=3&friend_source=0; __c=1701256413; __a=44892810.1701256413..1701256413.41.1.41.41; __zp_stoken__=fb10fNjbDl8OBccKzQSgIERIFCjgnNzYlMT42Kj08OzY2Nz49NjY%2FHDcmw6DCuBN3X2LDhgs2KTY3OTY2QDc8QRk2O8WCwrc1NTLDtsK1HXdeWMOHA8K%2FwrYNfwPDq8K2MsKtwrYLw47CvCkkKcK3QTc%2FN1bCvmDDggfCuVrDggfCtF3CuDc3NzYnMwMSWAwzN0pFXglJXkZUX00JTUpQKTc7OzbCmsSCw7YlNBERDAcRBgYHDAYQEA0RBxERDAcRBQUICwUuNsKdw4HDqlrCncKyw7bElcKYwrbDvEbCm1rCvsKtwpZPwqFRwqtDUsKowr5aTsKowp1ZXU1gVcKgwrjCqk5GUWzCvsOAVExJdldzacK9SREDEFpZMw9EX8OI"

city_list = [
    "101010100",  # 北京
    "101020100",  # 上海
    "101280100",  # 广州
    "101280600",  # 深圳
]
job_list = [
    '4A广告公司', '广告策划', '广告文案', '行销策划类广告人才'
]
job_item_list = []

url = "https://www.zhipin.com/wapi/zpgeek/search/joblist.json"
params = {
    "scene": "1",
    "query": "",
    "city": "101010100",
    "degree": "203,204,205",  # 设置学历，203为本科，204为硕士，205为博士
    "industry": "100101",  # 定义公司行业为广告
    "page": "1",
    "pageSize": "30",
}
headers = {
    'Host': 'www.zhipin.com',
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
    'Accept': 'application/json, text/plain, */*',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cookie': cookie
}
for city in city_list:
    for job in job_list:
        params.update({
            "query": job,
            "city": city
        })
        response = requests.get(url=url, headers=headers, params=params)
        job_data_list = response.json().get('zpData').get('jobList')
        for job_data in job_data_list:
            encryptJobId = job_data.get('encryptJobId')
            job_url = f"https://www.zhipin.com/job_detail/{encryptJobId}.html"
            job_response = requests.get(job_url, headers=headers)
            html_xpath = etree.HTML(job_response.text)
            job_require = "\n".join(html_xpath.xpath('//div[@class="job-sec-text"]/text()')).strip()
            job_item = job_data
            job_item['job_require'] = job_require
            print(job_item)
            job_item_list.append(job_item)
            time.sleep(2)

df = pandas.DataFrame(job_item_list)
df.to_excel("boss.xlsx", index=False)

